package common

import (
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
)

const ID_NULL = 0

// MINI APP ID
const (
	APP_ID_NULL = ID_NULL
	APP_ID_SYS  = 0

	APP_ID_APP_DEMO      = 999
	AP_ID_MINI_APP_START = 1000 //mini app id从1000开始
	APP_ID_MAIN_WEAPP    = 1000 //消费者端小程序
	APP_ID_TENANT_WEAPP  = 1001 //商户端主微信小程序
	APP_ID_CARD          = 1002 //卡片微应用

	APP_ID_MEMBER_CARD = 20000 //20000以上的ID范围属于2级应用（租户级应用）

)

type CHECK_MODE int
const (
	APP_PERMISSION_CHECK_MODE_DATATIME     CHECK_MODE = 0 //按日期时间检测
	APP_PERMISSION_CHECK_MODE_TIMES        CHECK_MODE = 1 //按使用次数检测
	APP_PERMISSION_CHECK_MODE_ALWAYS_VALID CHECK_MODE = 2 //不检查，总是有效, 目前这个check模式是给租户用的
	APP_PERMISSION_CHECK_MODE_DATETIME2 CHECK_MODE = 3 //日期时间检测

)

//APP处理函数中间件
func MiddlewareAppHandle(next fasthttp.RequestHandler, miniAppId int64) fasthttp.RequestHandler {
	return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
		ctxExt := lego.RequestCtxExtent{ctx}
		sessionData := GetSessionData(&ctxExt)
		if sessionData.SysTokenWho.MiniAppId != miniAppId {
			panic("NO permission for app")
		}
	})
}
